package com.harmonyos.studyharmonyos;

import com.harmonyos.studyharmonyos.slice.MailEditSlice;
import com.harmonyos.studyharmonyos.util.LogUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 分布式邮件迁移
 */
public class MigrateAbility extends Ability implements IAbilityContinuation {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MailEditSlice.class.getName());
        requestPermission();
    }

    private void requestPermission() {
        String[] permissions = {//敏感权限列表
                "ohos.permission.READ_USER_STORAGE",//读
                "ohos.permission.WRITE_USER_STORAGE",//写
                "ohos.permission.DISTRIBUTED_DATASYNC"//允许不同设备间的数据交换。
        };
        List<String> applyPermissions = new ArrayList<>();
        for (String element : permissions) {
            LogUtils.info(this.getClass().getSimpleName(), "check permisselement");
            if (verifySelfPermission(element) != 0) {
                if (canRequestPermission(element)) {
                    applyPermissions.add(element);
                } else {
                    LogUtils.info(this.getClass().getSimpleName(), "user deny permission");
                }
            } else {
                LogUtils.info(this.getClass().getSimpleName(), "user granted permission: " + element);
            }
        }
        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
